<?php
class UsersController extends AppController {

    var $name = 'Users';
    var $components = array('Auth', 'Email', 'RequestHandler', 'Cookie', 'PersistentValidation');
    var $helpers = array('Html', 'Form', 'Time');
    
    function beforeFilter() {
        $this->Auth->allow('register', 'recover', 'verify', 'about');
        if ($this->RequestHandler->isAjax()) {
            $this->Auth->allow('index');
        }
        parent::beforeFilter();
        $this->Auth->logoutRedirect = array('controller' => 'auctions', 'action' => 'index');
        // having a custom login function 
        $this->Auth->autoRedirect = false;
    }
    
    function beforeRender() {
        unset($this->data['User']['password']);
        unset($this->data['User']['password_confirm']);
    } 

    
    function index($id = null) {
    
        $user = $this->User->find('first', array('conditions' => array('User.id' => $id),'contain' => 'Profile'));       
        $this->set('title_for_layout', $user['Profile']['name']);
        $this->set('user', $user);
        if ($this->Auth->user('isAdmin')) {
            $this->loadModel('Product');
            $this->set('products', $this->Product->find('all', array('contain' => false))); 
        }
        
    }
    
    function updateToken() {
        $this->User->id = $this->Auth->user('id');
        if ($this->RequestHandler->isAjax()) {
            $this->autoRender = false;
            $this->autoLayout = false;
            
            if ($this->User->saveField('token', $_POST['token'])) {
                echo 'Updated';
            } else {
                echo 'Error';
            }
        } else {
            $this->User->set($this->data);
            $this->User->setValidation('updateToken');
            
            if (!empty($this->data) && $this->User->validates()) {
                $this->User->saveField('token', $this->data['User']['current']+ $this->data['User']['token']);     
            }
            $this->redirect($this->referer());
        }
    }

    
    function register() {
        if ($this->Auth->user()) {
            $this->redirect($this->referer());
        }
        
        $this->set('title_for_layout', 'Registration');
        
        if (!empty($this->data)) {
            if ($this->User->save($this->data)) {
                // Create new user profile with default email
                // REVIEW save() function
                $this->User->Profile->create();
                $this->User->Profile->saveField('user_id', $this->User->id);

                $this->Session->setFlash('Your account has been created. You may now login!');
                $this->redirect(array('controller' => 'profiles', 'action' => 'update', $this->User->Profile->id));
            }
        }
    }
    
    function login() {
        $this->set('title_for_layout', 'Member Login');  
        if (!empty($this->data) && $id = $this->Auth->user('id')) {
            
            $profile_id = $this->User->Profile->findByUserId($id, array('fields' => 'Profile.id'));

            $isRedirected = false;
            $this->User->id = $id;

            if ($this->User->field('lastlogin') == null) {
                //redirect to posting guide for the first timeout
                $isRedirected = true;
            }
            //save the lastlogin time            
            $this->User->saveField('lastlogin', date('Y-m-d H:i:s'));
                      
            // redirect users to Posting Guide
            if ($isRedirected) {
                $this->redirect(array('controller' => 'profiles', 'action' => 'update', $profile_id['Profile']['id']));
            } else {
                $url = array('controller' => 'auctions', 'action' => 'index');    
#                if ($this->Session->check('Auth.redirect')) {
#                    $url = $this->Session->read('Auth.redirect');
#                }
                $this->redirect($url);   
            }     
        }
        
        if ($this->Auth->user()) {
            $this->redirect($this->referer());
        }
    }
    
    function logout() {
        $this->redirect($this->Auth->logout());
    }

}
?>
